package com.ruoyi.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.app.domain.bo.AppApplyBo;
import com.ruoyi.app.domain.vo.AppApplyVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppApplyService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 提现账单
 *
 * @author app
 * @date 2025-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/apply")
public class AppApplyController extends BaseController {

    private final IAppUserService appUserService;
    private final IAppApplyService appApplyService;

    /**
     * 查询提现账单列表
     */
    @SaCheckPermission("system:apply:list")
    @GetMapping("/list")
    public TableDataInfo<AppApplyVo> list(AppApplyBo bo, PageQuery pageQuery) {
        TableDataInfo<AppApplyVo> page = appApplyService.queryPageList(bo, pageQuery);
        page.getData().forEach(e -> {
            AppUserVo appUserVo = appUserService.queryById(e.getUserId());
            if (Objects.nonNull(appUserVo)) {
                e.setUserName(appUserVo.getUserName());
            }
        });
        return page;
    }

    /**
     * 导出提现账单列表
     */
    @SaCheckPermission("system:apply:export")
    @Log(title = "提现账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppApplyBo bo, HttpServletResponse response) {
        List<AppApplyVo> list = appApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "提现账单", AppApplyVo.class, response);
    }

    /**
     * 获取提现账单详细信息
     *
     * @param applyId 主键
     */
    @SaCheckPermission("system:apply:query")
    @GetMapping("/{applyId}")
    public R<AppApplyVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long applyId) {
        return R.ok(appApplyService.queryById(applyId));
    }

    /**
     * 修改提现账单
     */
    @SaCheckPermission("system:apply:edit")
    @Log(title = "提现账单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody AppApplyBo bo) {
        if (bo.getApplyId() == null) {
            return R.fail("ID不能为空");
        }
        return toAjax(appApplyService.passOrder(bo));
    }

    /**
     * 删除提现账单
     *
     * @param applyIds 主键串
     */
    @SaCheckPermission("system:apply:remove")
    @Log(title = "提现账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] applyIds) {
        return toAjax(appApplyService.deleteWithValidByIds(Arrays.asList(applyIds), true) ? 1 : 0);
    }
}
